#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#include "Matrixkey.h"

unsigned char KeyNum;//矩阵键盘返回值接口
unsigned int Password,count;

void main()
{
	LCD_Init();//初始化
	LCD_ShowString(1,1,"MatrixKey:");//提示
	
	while(1)
	{
		KeyNum=MatrixKey();
		if(KeyNum)
		{
			if(KeyNum<=10)//按键1-10按下 ，依次输入
			{
				if(count<4)//如果输入次数小于4
				{
					Password*=10;         //密码左移
					Password+=KeyNum%10; 	//获取当前一位密码
					count++;              //+1
				}
				LCD_ShowNum(2,1,Password,4);//更新显示
			}
			if(KeyNum==12)  //如果s12按键按下，确认键
			{
				if(Password==103)//如果密码等于103
					{
						LCD_ShowString(1,13,"OK ");//正确的输出
						Password=0; //密码清零
						count=0;    //计次清零
						LCD_ShowNum(2,1,Password,4);//更新显示
					}		
				else
					{
						LCD_ShowString(1,13,"ERR");//错误时的输出
						Password=0;  //密码清零
						count=0;     //计次清零
						LCD_ShowNum(2,1,Password,4);//更新显示
					}					
			}
			if(KeyNum==11)//清零取消键
			{
				Password=0; //密码清零
				count=0;    //计次清零
			}
		}					
	}
}

